require 'rho/rhocontroller'
require 'helpers/browser_helper'
require 'helpers/application_helper'

class GradeController < Rho::RhoController
  include BrowserHelper
  include ApplicationHelper

  # GET /Grade
  def index
    $pagesize = 0 if $pagesize.nil?
        if ($pagesize > 0)
          @grades = Grade.find(:all, :per_page => $pagesize)
        else
          @grades = Grade.find(:all)
        end
        add_objectnotify(@grades)
        render
  end

  def page
    $pagesize = 0 if $pagesize.nil?
    page = @params['page'].nil? ? 1 : @params['page'].to_i
    @grades = Grade.find(:all, :per_page => $pagesize, :offset => page * $pagesize)
    render :layout => false, :use_layout_on_ajax => true
  end
  
  # GET /Grade/{1}
  def show
    @grade = Grade.find(@params['id'])
    if @grade
      render :action => :show, :back => url_for(:action => :index)
    else
      redirect :action => :index
    end
  end

  def async_show
      # This is an example of how to transition from an async http request. See show_callback below
      # for completed transition.
      Rho::AsyncHttp.get(
              :url =>  "http://10.10.10.154:3000/grades/#{@params['id']}.json",
              :callback => (url_for :action => :show_callback),
              :callback_param => caller_request_hash_to_query)
  
      # A Wait-Page response header notifies JavaScript that an async request has been spawned and that the current
      # rendering page should be treated as an interstitial page. Here we send back a waiting screen. Note that
      # interstitial pages are not treated as part of the browser's history.
      @response['headers']['Wait-Page'] = 'true'
      render :action => :waiting
    end
  
  # GET /Grade/new
  def new
    @grade = Grade.new
    render :action => :new, :back => url_for(:action => :index)
  end

  # GET /Grade/{1}/edit
  def edit
    @grade = Grade.find(@params['id'])
    if @grade
      render :action => :edit, :back => url_for(:action => :index)
    else
      redirect :action => :index
    end
  end

  # POST /Grade/create
  def create
    @grade = Grade.new(@params['grade'])
        @grade.save
    
        # immediately send to the server and show index after sync
        Grade.set_notification("/app/Grade/show_index_after_sync", caller_request_hash_to_query)
        SyncEngine.dosync_source(@grade.source_id)
    
        #@response['headers']['Wait-Page'] = 'true'
        #render :action => :waiting
        redirect :action => :index
  end

  # POST /Grade/{1}/update
  def update
    @grade = Grade.find(@params['id'])
    @grade.update_attributes(@params['grade'])

    # immediately send to the server and show index after sync
    Grade.set_notification("/app/Grade/show_index_after_sync", caller_request_hash_to_query)
    SyncEngine.dosync_source(@grade.source_id)

    #@response['headers']['Wait-Page'] = 'true'
    #render :action => :waiting
    redirect :action => :index
  end

  # POST /Grade/{1}/delete
  def delete
    @grade = Grade.find(@params['id'])
       @grade.destroy
   
       # immediately send to the server and show index after sync
       Grade.set_notification("/app/Grade/show_index_after_sync", caller_request_hash_to_query)
       SyncEngine.dosync_source(@grade.source_id)
   
       @response['headers']['Wait-Page'] = 'true'
       render :action => :waiting
    
  end
end
